package org.scboy.dbms.dubbo;

import java.util.ArrayList;
import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.api.IDynamicManager;
import org.scboy.dbms.dao.RequestDesignDao;
import org.scboy.dbms.dao.ServiceDesignDao;
import org.scboy.utils.caches.dbms.entity.DesignEntity;
import org.scboy.utils.entitys.coobi.Model;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class IDynamicManagerImpl implements IDynamicManager {
	
	@Autowired
	EntityFactory factory;
	
	@Override
	public DesignEntity getServiceDesign(String code, String appName) {
		
		ServiceDesignDao dao=factory.getDao(ServiceDesignDao.class);
		
		Model data= dao.get(Model.create().add("code", code).add("appName", appName).add("activity", 1));
		if(data==null) {
			return null;
		}else {
			return data.toEntity(DesignEntity.class);
		}
	}

	@Override
	public DesignEntity getRequestDesign(String code, String appName) {
		
		RequestDesignDao dao=factory.getDao(RequestDesignDao.class);
		Model data= dao.get(Model.create().add("code", code).add("appName", appName).add("activity", 1));
		if(data==null) {
			return null;
		}else {
			return data.toEntity(DesignEntity.class);
		}
	}

	@Override
	public List<DesignEntity> getServiceDesignList(String appName) {
		ServiceDesignDao dao=factory.getDao(ServiceDesignDao.class);
		List<Model> listModel=dao.select(Model.create().add("appName", appName).add("activity", 1));
		List<DesignEntity> result=new ArrayList<DesignEntity>();
		for (Model model : listModel) {
			result.add(model.toEntity(DesignEntity.class));
		}
		return result;
	}

	@Override
	public List<DesignEntity> getRequestDesignList(String appName) {
		RequestDesignDao dao=factory.getDao(RequestDesignDao.class);
		List<Model> listModel=dao.select(Model.create().add("appName", appName).add("activity", 1));
		List<DesignEntity> result=new ArrayList<DesignEntity>();
		for (Model model : listModel) {
			result.add(model.toEntity(DesignEntity.class));
		}
		return result;
	}

	@Override
	public DesignEntity getRequestDesignTest(String code, String appName) {
		RequestDesignDao dao=factory.getDao(RequestDesignDao.class);
		Model data= dao.get(Model.create().add("code", code).add("appName", appName));
		if(data==null) {
			return null;
		}else {
			return data.toEntity(DesignEntity.class);
		}
	}

	@Override
	public DesignEntity getServiceDesignTest(String code, String appName) {

		ServiceDesignDao dao=factory.getDao(ServiceDesignDao.class);
		
		Model data= dao.get(Model.create().add("code", code).add("appName", appName));
		if(data==null) {
			return null;
		}else {
			return data.toEntity(DesignEntity.class);
		}
	}

}
